class FileBlockDev:
    def __init__(self, file_path, block_size, num_blocks):
        self.file_path = file_path
        self.block_size = block_size
        self.num_blocks = num_blocks
        self.file_size = block_size * num_blocks

        # Создаем или открываем файл
        self.file = open(file_path, 'ab+')

    def readblocks(self, block_num, buf):
        self.file.seek(block_num * self.block_size)
        data = self.file.read(self.block_size)
        for i in range(len(buf)):
            if i < len(data):
                buf[i] = data[i]
            else:
                buf[i] = 0

    def writeblocks(self, block_num, buf):
        self.file.seek(block_num * self.block_size)
        self.file.write(bytes(buf))
        self.file.flush()

    def ioctl(self, op, arg):
        if op == 4:  # get number of blocks
            return self.num_blocks
        if op == 5:  # get block size
            return self.block_size

    def __del__(self):
        self.file.close()